const fs = wx.getFileSystemManager()
Page({
  data: {
    schema:[
      {
        label:'文件路径',
        prop:'filePath',
        type:'text'
      },{
        label:'内容',
        prop:'data',
        type:'text'
      },{
        label:'同步模式',
        prop:'isSync',
        type:'switch'
      }
    ],
    formData:{
      filePath: `${wx.env.USER_DATA_PATH}/test.txt`,
      data:'',
      isSync: false,
    },
    result:'',
    title:'文件管理',
    apiName:'',
    fd:null
  },
  onLoad(option) {
    this.setData({
      zh:option.zh,
      remark:option.remark,
      weburl:option.docUrl
    })
  },
  getFormData(e) {
    this.setData({
      formData:e.detail.formData
    })
  },
  writeFile(t) {
    if(this.data.formData.isSync) {
      const res = fs.writeFileSync(this.data.formData.filePath,'some text or arrayBuffer','utf8')
      console.log(res)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.writeFile({
        filePath:this.data.formData.filePath,
        data:'some text or arrayBuffer',
        encoding:'utf8',
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  readFile(t){
    if(this.data.formData.isSync) {
      const res = fs.readFileSync(this.data.formData.filePath,'utf8')
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.readFile({
        filePath:this.data.formData.filePath,
        encoding:'utf8',
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  appendFile(t) {
    if(this.data.formData.isSync) {
      const res = fs.appendFileSync(this.data.formData.filePath,this.data.formData.data,'utf8')
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.appendFile({
        filePath:this.data.formData.filePath,
        data:this.data.formData.data,
        encoding:'utf8',
        complete:(res)=>{
          this.setData({
            result:res,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  access(t) {
    if(this.data.formData.isSync) {
      const res = fs.accessSync(this.data.formData.filePath)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.access({
        path:this.data.formData.filePath,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  readdir(t) {
    if(this.data.formData.isSync) {
      const res = fs.readdirSync(this.data.formData.filePath)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.readdir({
        dirPath:this.data.formData.filePath,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  mkdir(t) {
    if(this.data.formData.isSync) {
      const res = fs.mkdirSync(this.data.formData.filePath,true)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.mkdir({
        dirPath:this.data.formData.filePath,
        recursive:true,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  rmdir(t) {
    if(this.data.formData.isSync) {
      const res = fs.rmdirSync(this.data.formData.filePath,true)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.rmdir({
        dirPath:this.data.formData.filePath,
        recursive:true,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  getFileInfo(t){
    fs.getFileInfo({
      filePath:this.data.formData.filePath,
      complete:(e)=>{
        this.setData({
          result:e,
          apiName:t.currentTarget.dataset.apiname
        })
      }
    })
  },
  getSavedFileList(t){
    fs.getSavedFileList({
      complete:(e)=>{
        this.setData({
          result:e,
          apiName:t.currentTarget.dataset.apiname
        })
      }
    })
  },
  copyFile(t) {
    if(this.data.formData.isSync) {
      const res = fs.copyFileSync(this.data.formData.filePath,this.data.formData.data)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.copyFile({
        srcPath:this.data.formData.filePath,
        destPath:this.data.formData.data,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  truncate(t) {
    if(this.data.formData.isSync) {
      const res = fs.truncateSync({
        filePath:this.data.formData.filePath,
        length:16
      })
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.ftruncate({
        filePath:this.data.formData.filePath,
        length:16,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  unlink(t){
    if(this.data.formData.isSync) {
      const res = fs.unlinkSync(this.data.formData.filePath)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.unlink({
        filePath:this.data.formData.filePath,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  open(t) {
    if(this.data.formData.isSync) {
      const res = fs.openSync({
        filePath:this.data.formData.filePath,
        flag:'a+'
      })
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.open({
        filePath:this.data.formData.filePath,
        flag:'a+',
        success:(e)=>{
          this.setData({
            fd:e.fd
          })
        },
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  fstat(t) {
    if(this.data.formData.isSync) {
      const res = fs.fstatSync({fd:this.data.fd})
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.fstat({
        fd:this.data.fd,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  stat(t) {
    if(this.data.formData.isSync) {
      const res = fs.statSync(this.data.formData.filePath)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.stat({
        path:this.data.formData.filePath,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  ftruncate(t) {
    if(this.data.formData.isSync) {
      const res = fs.ftruncateSync({fd:this.data.fd,length:16})
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.ftruncate({
        fd:this.data.fd,
        length:16,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  read(t) {
    const ab = new ArrayBuffer(1024)
    if(this.data.formData.isSync) {
      const res = fs.readSync({fd:this.data.fd,arrayBuffer:ab,length:32})
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.read({
        fd:this.data.fd,
        arrayBuffer:ab,
        length:32,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  write(t) {
    if(this.data.formData.isSync) {
      const res = fs.writeSync({
        fd:this.data.fd,
        data:this.data.formData.data,
        encoding:'utf8',
      })
      console.log(res)
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.write({
        fd:this.data.fd,
        data:this.data.formData.data,
        encoding:'utf8',
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  readCompressedFile(t) {
    if(this.data.formData.isSync) {
      const res = fs.readCompressedFileSync({
        filePath:this.data.formData.filePath,
        compressionAlgorithm:'br',
      })
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.readCompressedFile({
        filePath:this.data.formData.filePath,
        compressionAlgorithm:'br',
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  readZipEntry(t){
    fs.readZipEntry({
      filePath:this.data.formData.filePath,
      encoding:'utf-8',
      entries:'all',
      complete:(e)=>{
        this.setData({
          result:e,
          apiName:t.currentTarget.dataset.apiname
        })
      }
    })
  },
  unzip(t){
    fs.unzip({
      zipFilePath:this.data.formData.filePath,
      targetPath:this.data.formData.data,
      complete:(e)=>{
        this.setData({
          result:e,
          apiName:t.currentTarget.dataset.apiname
        })
      }
    })
  },
  removeSavedFile(t) {
    fs.removeSavedFile({
      filePath:this.data.formData.filePath,
      complete:(e)=>{
        this.setData({
          result:e,
          apiName:t.currentTarget.dataset.apiname
        })
      }
    })
  },
  rename(t){
    if(this.data.formData.isSync) {
      const e = fs.renameSync(this.data.formData.filePath,this.data.formData.data)
      this.setData({
        result:e,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.rename({
        oldPath:this.data.formData.filePath,
        newPath:this.data.formData.data,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  saveFile(t) {
    if(this.data.formData.isSync) {
      const e = fs.saveFileSync(this.data.formData.filePath,this.data.formData.data)
      this.setData({
        result:e,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else {
      fs.saveFile({
        filePath:this.data.formData.filePath,
        tempFilePath:this.data.formData.data,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  close(t){
    if(this.data.formData.isSync) {
      const res = fs.closeSync({fd:this.data.fd})
      this.setData({
        result:res,
        apiName:t.currentTarget.dataset.apiname + 'Sync'
      })
    }else{
      fs.close({
        fd:this.data.fd,
        complete:(e)=>{
          this.setData({
            result:e,
            apiName:t.currentTarget.dataset.apiname
          })
        }
      })
    }
  },
  isDirectory(t){
    const res = fs.statSync(this.data.formData.filePath)
    console.log(res.isDirectory())
    this.setData({
      result:res.isDirectory(),
      apiName:t.currentTarget.dataset.apiname
    })
  },
  isFile(t){
    const res = fs.statSync(this.data.formData.filePath)
    console.log(res)
    this.setData({
      result:res.isFile(),
      apiName:t.currentTarget.dataset.apiname
    })
  },
})
